package com.vectras.vm.utils;

import static android.content.Intent.ACTION_VIEW;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.preference.PreferenceManager;

import com.google.android.material.color.MaterialColors;
import com.vectras.vm.R;

public class DialogUtils {

    public static void oneDialog(Activity _context, String _title, String _message, String _textPositiveButton, boolean _isicon, int _iconid, boolean _cancel, Runnable _onPositive, Runnable _onDismiss) {
        View buttonsView = LayoutInflater.from(_context).inflate(R.layout.dialog_layout, null);

        AlertDialog dialog = new AlertDialog.Builder(_context).create();
        dialog.setCancelable(_cancel);
        dialog.setView(buttonsView);

        ImageView icon = buttonsView.findViewById(R.id.icon);
        TextView title = buttonsView.findViewById(R.id.tv_title);
        TextView content = buttonsView.findViewById(R.id.tv_content);
        TextView positiveButton = buttonsView.findViewById(R.id.positiveButton);
        TextView negativeButton = buttonsView.findViewById(R.id.negativeButton);
        TextView neutralButton = buttonsView.findViewById(R.id.neutralButton);

        if (_isicon) {
            icon.setImageResource(_iconid);
        } else {
            icon.setVisibility(View.GONE);
        }

        if (UIUtils.isUsingThemeNightMode()
                || !UIUtils.isColorLight(MaterialColors.getColor(positiveButton, com.google.android.material.R.attr.colorPrimaryContainer)))
            positiveButton.setTextColor(Color.WHITE);

        title.setText(_title);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Spannable sp = (Spannable) (isHTML(_message) ?
                    Html.fromHtml(_message, Html.FROM_HTML_MODE_LEGACY) :
                    new SpannableString(_message));
            Linkify.addLinks(sp, Linkify.ALL);
            content.setText(sp);
            content.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            content.setText(_message);
        }


        positiveButton.setText(_textPositiveButton);
        positiveButton.setBackgroundResource(R.drawable.dialog_shape_single_button);
        negativeButton.setVisibility(View.GONE);
        neutralButton.setVisibility(View.GONE);

        positiveButton.setOnClickListener(v -> {
            if (_onPositive != null) _onPositive.run();
            dialog.dismiss();
        });

//        dialog.setPositiveButton(_textPositiveButton, (dialog2, which) -> {
//            if (_onPositive != null) _onPositive.run();
//            dialog2.dismiss();
//        });
        dialog.setOnDismissListener(dialog1 -> {
            if (_onDismiss != null) _onDismiss.run();
        });
        dialog.show();
    }

    public static void twoDialog(Activity _context, String _title, String _message, String _textPositiveButton, String _textNegativeButton, boolean _isicon, int _iconid, boolean _cancel, Runnable _onPositive, Runnable _onNegative, Runnable _onDismiss) {
        View buttonsView = LayoutInflater.from(_context).inflate(R.layout.dialog_layout, null);

        AlertDialog dialog = new AlertDialog.Builder(_context).create();
        dialog.setCancelable(_cancel);
        dialog.setView(buttonsView);

        ImageView icon = buttonsView.findViewById(R.id.icon);
        TextView title = buttonsView.findViewById(R.id.tv_title);
        TextView content = buttonsView.findViewById(R.id.tv_content);
        TextView positiveButton = buttonsView.findViewById(R.id.positiveButton);
        TextView negativeButton = buttonsView.findViewById(R.id.negativeButton);
        TextView neutralButton = buttonsView.findViewById(R.id.neutralButton);

        if (_isicon) {
            icon.setImageResource(_iconid);
        } else {
            icon.setVisibility(View.GONE);
        }

        if (UIUtils.isUsingThemeNightMode()
                || !UIUtils.isColorLight(MaterialColors.getColor(positiveButton, com.google.android.material.R.attr.colorPrimaryContainer))) {
            positiveButton.setTextColor(Color.WHITE);
            negativeButton.setTextColor(Color.WHITE);
        }

        title.setText(_title);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Spannable sp = (Spannable) (isHTML(_message) ?
                    Html.fromHtml(_message, Html.FROM_HTML_MODE_LEGACY) :
                    new SpannableString(_message));
            Linkify.addLinks(sp, Linkify.ALL);
            content.setText(sp);
            content.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            content.setText(_message);
        }

        positiveButton.setText(_textPositiveButton);
        negativeButton.setText(_textNegativeButton);
        negativeButton.setBackgroundResource(R.drawable.dialog_shape_bottom_button);
        neutralButton.setVisibility(View.GONE);

        positiveButton.setOnClickListener(v -> {
            if (_onPositive != null) _onPositive.run();
            dialog.dismiss();
        });

        negativeButton.setOnClickListener(v -> {
            if (_onNegative != null) _onNegative.run();
            dialog.dismiss();
        });
//        dialog.setPositiveButton(_textPositiveButton, (dialog2, which) -> {
//            if (_onPositive != null) _onPositive.run();
//            dialog2.dismiss();
//        });
//        dialog.setNegativeButton(_textNegativeButton, (dialog3, which) -> {
//            if (_onNegative != null) _onNegative.run();
//            dialog3.dismiss();
//        });
        dialog.setOnDismissListener(dialog1 -> {
            if (_onDismiss != null) _onDismiss.run();
        });
        dialog.show();
    }

    public static void threeDialog(Activity _context, String _title, String _message, String _textPositiveButton, String _textNegativeButton, String _textNeutralButton, boolean _isicon, int _iconid, boolean _cancel, Runnable _onPositive, Runnable _onNegative, Runnable _onNeutral, Runnable _onDismiss) {
        View buttonsView = LayoutInflater.from(_context).inflate(R.layout.dialog_layout, null);

        AlertDialog dialog = new AlertDialog.Builder(_context).create();
        dialog.setCancelable(_cancel);
        dialog.setView(buttonsView);

        ImageView icon = buttonsView.findViewById(R.id.icon);
        TextView title = buttonsView.findViewById(R.id.tv_title);
        TextView content = buttonsView.findViewById(R.id.tv_content);
        TextView positiveButton = buttonsView.findViewById(R.id.positiveButton);
        TextView negativeButton = buttonsView.findViewById(R.id.negativeButton);
        TextView neutralButton = buttonsView.findViewById(R.id.neutralButton);

        if (_isicon) {
            icon.setImageResource(_iconid);
        } else {
            icon.setVisibility(View.GONE);
        }

        title.setText(_title);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Spannable sp = (Spannable) (isHTML(_message) ?
                    Html.fromHtml(_message, Html.FROM_HTML_MODE_LEGACY) :
                    new SpannableString(_message));
            Linkify.addLinks(sp, Linkify.ALL);
            content.setText(sp);
            content.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            content.setText(_message);
        }

        if (UIUtils.isUsingThemeNightMode()
                || !UIUtils.isColorLight(MaterialColors.getColor(positiveButton, com.google.android.material.R.attr.colorPrimaryContainer))) {
            positiveButton.setTextColor(Color.WHITE);
            negativeButton.setTextColor(Color.WHITE);
            neutralButton.setTextColor(Color.WHITE);
        }

        positiveButton.setText(_textPositiveButton);
        negativeButton.setText(_textNegativeButton);
        neutralButton.setText(_textNeutralButton);

        positiveButton.setOnClickListener(v -> {
            if (_onPositive != null) _onPositive.run();
            dialog.dismiss();
        });

        negativeButton.setOnClickListener(v -> {
            if (_onNegative != null) _onNegative.run();
            dialog.dismiss();
        });

        neutralButton.setOnClickListener(v -> {
            if (_onNeutral != null) _onNeutral.run();
            dialog.dismiss();
        });

//        dialog.setPositiveButton(_textPositiveButton, (dialog2, which) -> {
//            if (_onPositive != null) _onPositive.run();
//            dialog2.dismiss();
//        });
//        dialog.setNegativeButton(_textNegativeButton, (dialog3, which) -> {
//            if (_onNegative != null) _onNegative.run();
//            dialog3.dismiss();
//        });
//        dialog.setNeutralButton(_textNeutralButton, (dialog4, which) -> {
//            if (_onNeutral != null) _onNeutral.run();
//            dialog4.dismiss();
//        });
        dialog.setOnDismissListener(dialog1 -> {
            if (_onDismiss != null) _onDismiss.run();
        });
        dialog.show();
    }

    public static void joinTelegram(Activity _activity) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(_activity);
        if (!prefs.getBoolean("tgDialog", false)) {
            threeDialog(_activity, _activity.getResources().getString(R.string.join_us_on_telegram),
                    _activity.getResources().getString(R.string.join_us_on_telegram_where_we_publish_all_the_news_and_updates_and_receive_your_opinions_and_bugs),
                    _activity.getResources().getString(R.string.join), _activity.getResources().getString(R.string.cancel), _activity.getResources().getString(R.string.dont_show_again),
                    true, R.drawable.send_24px, true,
                    () -> {
                        String tg = "https://t.me/vectras_os";
                        Intent f = new Intent(ACTION_VIEW);
                        f.setData(Uri.parse(tg));
                        _activity.startActivity(f);
                    }, null,
                    () -> {
                        SharedPreferences.Editor edit = prefs.edit();
                        edit.putBoolean("tgDialog", true);
                        edit.apply();
                    }, null);
        }
    }

    public static void needInstallTermuxX11(Activity _activity) {
        twoDialog(_activity, _activity.getResources().getString(R.string.action_needed),
                _activity.getResources().getString(R.string.need_install_termux_x11_content),
                _activity.getResources().getString(R.string.install), _activity.getResources().getString(R.string.cancel),
                true, R.drawable.warning_24px, true,
                () -> {
                    String tg = "https://github.com/termux/termux-x11/releases";
                    Intent f = new Intent(ACTION_VIEW);
                    f.setData(Uri.parse(tg));
                    _activity.startActivity(f);
                }, null, null);
    }

    public static void fileDeletionResult(Activity activity, boolean isCompleted) {
        if (isCompleted) {
            DialogUtils.oneDialog(
                    activity,
                    activity.getString(R.string.done),
                    activity.getString(R.string.file_deleted),
                    activity.getString(R.string.ok),
                    true,
                    R.drawable.check_24px,
                    true,
                    null,
                    null
            );
        } else {
            DialogUtils.oneDialog(
                    activity,
                    activity.getString(R.string.oops),
                    activity.getString(R.string.delete_file_failed_content),
                    activity.getString(R.string.ok),
                    true,
                    R.drawable.error_96px,
                    true,
                    null,
                    null
            );
        }
    }

    public static boolean isHTML(String content) {
        return content.contains("<") && content.contains("</") && content.contains(">");
    }
}
